import { Injectable } from '@angular/core';
import { Graph } from '@antv/g6';
import { BehaviorSubject, Subject } from 'rxjs';
import { DataRegister, FlowNode } from 'src/app/models';

@Injectable({
  providedIn: 'root'
})
export class WorkflowPageService {

  private _pageGraph: Graph;
  private _currentNodeId: string;
  private _currentDataRegisterId: string;
  private _workflowId: string;
  private _currentChildDataRegisterId: string;

  get pageGraph() {
    return this._pageGraph;
  }

  set pageGraph(graph: Graph) {
    this._pageGraph = graph;
  }

  get currentNodeId() {
    return this._currentNodeId;
  }

  set currentNodeId(nodeId: string) {
    this._currentNodeId = nodeId;
  }

  get currentDataRegisterId() {
    return this._currentDataRegisterId;
  }

  set currentDataRegisterId(registerId: string) {
    this._currentDataRegisterId = registerId;
  }

  get workflowId() {
    return this._workflowId;
  }

  set workflowId(id: string) {
    this._workflowId = id;
  }

  get currentChildDataRegisterId() {
    return this._currentChildDataRegisterId;
  }

  set currentChildDataRegisterId(registerId: string) {
    this._currentChildDataRegisterId = registerId;
  }

  workflowUpdated: BehaviorSubject<DataRegister> = new BehaviorSubject<DataRegister>(null);
  createChildFlow: Subject<string> = new Subject<string>();
  deleteChildFlow: Subject<string> = new Subject<string>();
  nodeValidChanged: Subject<{ id: string; name: string; disabled: boolean; }[]> = new Subject<{ id: string; name: string; disabled: boolean; }[]>();
  flowNodeUpdate: BehaviorSubject<{
    flowNodeId: string;
    nodeName: string;
    status: string;
    updateTime: number;
    flowNode: FlowNode;
    executor?: {
      userId: string;
      name: string;
    }
  }> = new BehaviorSubject(null);

  // 审核相关事件

  // 初始化审核
  auditInit: Subject<any> = new Subject();
  // 提交审核
  auditSubmit: Subject<any> = new Subject();
  auditFinish$: Subject<any> = new Subject();
  auditChanged$: Subject<any> = new Subject();
  auditCurrentItemChanged$: Subject<any> = new Subject();

  onSaveData$: Subject<any> = new Subject();


  notifyWorkflowUpdated(dataRegister: DataRegister) {
    this.workflowUpdated.next(dataRegister);
  }

  notifyFlowNodeUpdate(obj, flowNode) {
    if (obj) {
      this.flowNodeUpdate.next({
        flowNode,
        ...obj
      });
    }
    else {
      this.flowNodeUpdate.next(null);
    }

  }

  notifyNodeValidChanged(data: { id: string; name: string; disabled: boolean; }[]) {
    this.nodeValidChanged.next(data);
  }
  /**
   * 通知创建子流程的dataRegister
   */
  createChildFlowDataregister(childFlowId: string) {
    this.createChildFlow.next(childFlowId);
  }
  /**
   * 通知删除子流程的dataRegister
   * @param childFlowId 子流程id
   */
  deleteChildFlowDataRegister(childFlowId: string) {
    this.deleteChildFlow.next(childFlowId);
  }
}
